'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 竞技场商城购买
 * @author agl
 */

var _ = require('lodash');
var async = require('async');
var moment = require('moment');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var configUtil = require('../../../../util/configUtil');
var PlayerManager = require('../../../../libs/playerManager');
var dataApi = require('../../../../libs/dataApi');
var taskService = require('../../../../services/taskService');
var roleService = require('../../../../services/roleService');
var rewardService = require('../../../../services/rewardService');
var arenaConf = require('../../../../config/arenaConf.js');

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var shop_pos = msg.shop_pos;

    if (!shop_pos) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var player_manager = new PlayerManager(role_id);
    var reward_list;
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['arena', 'build', 'soldier', 'role', 'bag', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var arena_model = player_manager.arena;
                var shop_item = arena_model.getShopItem(shop_pos);
                if (!shop_item) {
                    return next(null, {
                        code: Code.ARENA_INVALID_SHOP_POS,
                    });
                }

                if (!arena_model.decShopItemCount(shop_pos)) {
                    return next(null, {
                        code: Code.ARENA_SHOP_ITEM_BUY_LIMIT,
                    });
                }

                // 取出对应商城物品信息
                var shop_item_conf = arenaConf.getShopItem(shop_item.shop_id);
                if (!shop_item_conf) {
                    return next(null, {
                        code: Code.BAG_ITEM_NOT_EXIST,
                    });
                }
                var reward_info = {
                    reward: shop_item_conf.reward,
                    reward_type: shop_item_conf.reward_type,
                    reward_num: shop_item_conf.reward_num,
                    reward_value: shop_item_conf.reward_value,
                    reward_hole_num: shop_item_conf.reward_hole_num
                };

                reward_list = [reward_info];

                // 扣除相应竞技币
                var arena_coin_cost = shop_item_conf.arena_coin_cost;
                if (!arena_model.useArenaCoin(arena_coin_cost)) {
                    return next(null, {
                        code: Code.ARENA_NOT_ENOUGH_COIN,
                    });
                }

                // 发放物品
                rewardService.addRewardListToRole(player_manager, reward_list, moduleType.MDL_ARENA, operationType.OPR_ARENA_SHOP_BUY);

                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }

            return next(null, {
                code: Code.OK,
                result: {
                    reward_list: reward_list
                }
            });
        }
    );
};
